package com;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;


import com.ami.weather.R;
import com.jy.unkown.AD.UnkownBannerAD;
import com.jy.unkown.AD.UnkownFeedAd;
import com.jy.unkown.AdReqCallBack;
import com.jy.unkown.inter.IBannerCallBack;
import com.jy.unkown.view.UnkownADView;
import com.jy.utils.call.NoDoubleClick;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

public class FeedActivity extends BBActivity {
    FrameLayout feedLayout;
    static final String TAG="Feed";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_feed);
        feedLayout = findViewById(R.id.feedLayout);
        findViewById(R.id.jiazai).setOnClickListener(new NoDoubleClick() {
            @Override
            public void noDoubleCall(View view) {
                loadAd();
            }
        });
        findViewById(R.id.zhanshi).setOnClickListener(new NoDoubleClick() {
            @Override
            public void noDoubleCall(View view) {
                zhanshi();
            }
        });
    }


    UnkownFeedAd unkownFeedAd;

    IBannerCallBack adCallBack = new IBannerCallBack() {
        @Override
        public void click() {
            Log.e(TAG, "点击");
        }

        @Override
        public void exposure() {
            Log.e(TAG, "曝光成功");
        }

        @Override
        public void err() {
            Log.e(TAG, "曝光失败");
        }
    };


    AtomicBoolean isRequesting = new AtomicBoolean(false);
    AtomicBoolean isRequestSucc = new AtomicBoolean(false);

    AdReqCallBack adReqCallBack = new AdReqCallBack() {
        @Override
        public void reqSucc() {
            Log.e(TAG, "拉取广告成功");
            isRequestSucc.set(true);
            isRequesting.set(false);
        }

        @Override
        public void reqErr() {
            Log.e(TAG, "拉取广告失败");
            isRequesting.set(false);
        }
    };


    protected void loadAd() {
        if (isRequesting.get()) {
            return;
        }
        if (unkownFeedAd == null) {
            unkownFeedAd = new UnkownFeedAd();
        }
        unkownFeedAd.setAdCallBack(adCallBack);
        unkownFeedAd.setRequestId(UUID.randomUUID().toString());
        unkownFeedAd.setAdReqCallBack(adReqCallBack);
        isRequesting.set(true);
        isRequestSucc.set(false);
        unkownFeedAd.setAppId(Key.APPID);
        unkownFeedAd.setAdId(Key.FEED_ID);
        unkownFeedAd.setPrice(0);
        unkownFeedAd.loadAd();
    }


    protected void zhanshi() {
        if (!isRequestSucc.get()) {
            showToast("广告未请求成功");
            return;
        }
        try {
            UnkownADView view = unkownFeedAd.getADView(this);
            feedLayout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            view.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
